<?php
namespace backend\controllers;

use Yii;
use yii\web\AdminController;
use backend\models\GoodsLineSync;
use common\helps\tools;

class PushGoodsController extends AdminController
{
    public $layout = false;
    //业务关键字
    public $service = 'PushGoods';
    
    public function actionIndex()
    {
        return $this->render('index');
    }
    
    public function actionGetPushGoods(){
        $data = Yii::$app->request->post();
        $push_goods_rule = tools::getPushGoodsRule();
        if (count($push_goods_rule) > 0){
            $goods_name_cn = $data['goods_name_cn'];
            $reservationtime = $data['reservationtime'];
            $consult = $data['consult'];
            $push_goods = GoodsLineSync::find()->select('id,update_time,item_no,list_price,item_price,item_desc,itemtype,item_group,item_spec,item_stuff,brand_code,brand_ename,brand_cname,item_sf,minin_qty,color,item_country,item_state,itemname,itemmodel,itemseries,curinccode,preinccode,itememployee,consult');
            if ($goods_name_cn){
                $push_goods = $push_goods->where(['like','item_desc',$goods_name_cn]);
            }
            if ($reservationtime){
                $date_arr = explode(' - ', $reservationtime);
                
                if (count($date_arr) == 2){
                    $date_arr[0] = $date_arr[0] .= ' 00:00:00';
                    $date_arr[1] = date('Y-m-d 00:00:00',strtotime($date_arr[1]. "+ 1 day"));
                    $push_goods = $push_goods->andWhere('update_time BETWEEN "'.$date_arr[0].'" AND "'.$date_arr[1].'"');
                }
            }
            if ($consult === '0' || $consult){
                $push_goods = $push_goods->andWhere('consult=:consult',[':consult'=>$consult]);
            }
            $goods_genre_key = array_keys($GLOBALS['_GOODS_GENRE_TYPE']);
            $sql = '';
            if (in_array(-1, $push_goods_rule)){
                $key=array_search(-1 ,$push_goods_rule);
                if (isset($key) && !empty($key)){
                    unset($push_goods_rule[$key]);
                }
                unset($goods_genre_key[0],$goods_genre_key[1]);
                $itemtype_in = array_diff($goods_genre_key, $push_goods_rule);
                if (count($itemtype_in) > 0){
                    $sql .= 'itemtype not in ('.implode(',', $itemtype_in).')';
                }
            }else{
                $sql .= 'itemtype in ('.implode(',', $push_goods_rule).')';
            }
            if (strlen($sql) > 0){
                $push_goods = $push_goods->andWhere($sql);
            }
            $count = $push_goods->count();
            
            $sort_string = empty($data['columns'][$data['order'][0]['column']]['data']) ? 'id' : $data['columns'][$data['order'][0]['column']]['data'];
            $list = $push_goods->offset($data['start'])->limit($data['length'])->orderBy(
                [$sort_string=>$data['order'][0]['dir'] == 'asc' ? SORT_ASC : SORT_DESC])->asArray()->all();
        }else{
            $count = 0;
        }
//         $list = $push_goods->offset($data['start'])->limit($data['length'])->orderBy(['id'=>SORT_DESC])->asArray()->all();
        $result['draw'] = intval($data['draw']);
        $result['recordsTotal'] = intval($count);
        $result['recordsFiltered'] = intval($count);
        $result['data'] = $list;
        return \yii\helpers\Json::encode($result);
    }
    
    public function actionGetPushGoodsDetail(){
        $data = Yii::$app->request->post();
        $where = ['id'=>$data['id']];
        $result = GoodsLineSync::find()->where($where)->asArray()->one();
        $this->retJson($result,200);
    }
    
    public function actionChangePushGoodsConsult(){
        $model = new GoodsLineSync();
        $data = Yii::$app->request->post();
        $where = ['id'=>$data['line_sync_id']];
        $data_now = [
            'consult'=>1,
            'consult_person'=>$model->getCreator(),
            'update_time'=>date('Y-m-d H:i:s')
        ];
        $ret = $model::updateAll($data_now,$where);
        if(!$ret){
            throw new \Exception('操作失败');
        }
        $this->retJson('操作成功',200);
    }
}